package org.wyz.Singleton;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;

public class Singleton {

    public static void main(String[] args) throws InterruptedException {
        int nThread = 10000;
        CountDownLatch countDownLatch = new CountDownLatch(nThread);

        LocalDateTime time1 = LocalDateTime.now();

        for (int i = 0; i < nThread; i++) {
            new Thread(() -> {
                Car.getInstance();
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        LocalDateTime time2 = LocalDateTime.now();

        Duration d = Duration.between(time1, time2);
        System.out.println(d.getNano());
    }


}

class Car {
    public static Car car;

    private Car() {
        System.out.println("新建了一个实例...");
    }

    //    public static synchronized Car getInstance() {
    public static Car getInstance() {
        if (car != null) {
            return car;
        }

        synchronized (Car.class) {
            car = new Car();
            return car;
        }
    }
}
